package com.sx.mvvmlibs.utils

import com.sx.mvvmlibs.base.BaseApp

/**
 * @Author: YY
 * @Date: 2022/1/6
 * @Describe: 密度转换工具类
 */
object DensityUtils {

    /**
     * dp 转 px
     */
    fun dp2px(dpValue: Float): Int {
        val scale = BaseApp.getInstance().resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    /**
     * px 转 dp
     */
    fun px2dp(pxValue: Float): Int {
        val scale = BaseApp.getInstance().resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     * 将 px 值转换为 sp值
     */
    fun px2sp(pxValue: Float): Int {
        val fontScale = BaseApp.getInstance().resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    /**
     * 将 sp 值转换为 px 值
     */
    fun sp2px(spValue: Float): Int {
        val fontScale = BaseApp.getInstance().resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }
}